﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Configuration;
using DSPL.Contracts;
using DSPL.Contracts.Common;
using DSPL.Contracts.Interfaces;
using Microsoft.Practices.Unity;


namespace DSPL.Common.CommonControls.Controls
{
           public class TxtCurCode: TxtMasterData
    {
               private object _bindDate;
               private object _bindRate;

               #region Properties
               public object BindDate
               {
                   get { return _bindDate; }
                   set { _bindDate = value; }
               }

               public object BindRate
               {
                   get { return _bindRate; }
                   set { _bindRate = value; }
               }

               public ICommonRules CurCommonRules
               {
                   get;
                   set;
               }

               #endregion 

               #region
               public TxtCurCode()
               {
                   HelpCode = "HelpCurrency";
                   IsMandatory = true;
                   SelectTable = "fCurrency";
                   KeyField = "CurCode";
                

               }
                  #endregion

               protected override void OnLostFocus(RoutedEventArgs e)
               {
                   base.OnLostFocus(e);

                   if (BindDate != null && BindRate != null)
                   {
                       ResolveDependencies();
                       string baseCurrency = GlobalVariables.BaseCurrency;
                       double currencyRate;

                       if (CurCommonRules != null)
                       {
                           currencyRate = Convert.ToDouble(CurCommonRules.GetLatestCurrencyRate(this.Text, baseCurrency, Convert.ToDateTime(((TxtDate)BindDate).Text)));
                           ((TextBox)BindRate).Text = Convert.ToString(currencyRate);

                           if (this.Text.Trim() == baseCurrency)
                           {
                               ((TextBox)BindRate).IsEnabled = false;
                               ((TextBox)BindRate).Text = "1.00";
                           }

                           else
                           {
                               if (this.Text.Trim() == string.Empty)
                               {
                                   ((TextBox)BindRate).IsEnabled = false;
                               }
                               else
                               {
                                   ((TextBox)BindRate).IsEnabled = true;
                                   ((TextBox)BindRate).Focus();
                               }
                           }

                       }
                   }
               }

               protected override void ResolveDependencies()
               {
                   base.ResolveDependencies();
                   if (base.MasterUnityContainer != null)
                   {
                       IUnityContainer masterUnityContainer = (IUnityContainer)base.MasterUnityContainer;
                       if (CurCommonRules == null)
                       {
                           CurCommonRules = masterUnityContainer.Resolve<ICommonRules>();
                       }
                   }
               }
    }
}
